package com.czxy.service;

import com.czxy.dao.UserMapper;
import com.czxy.domain.User;
import com.czxy.utils.MyBatisUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

public class UserService {



    // 登录, 登录成功返回 指定的user对象 失败返回null
    public User login(User user){
        UserMapper mapper = MyBatisUtils.getMapper(UserMapper.class);

        Example example = new Example(User.class);
        Example.Criteria ct = example.createCriteria();
        ct.andEqualTo("username", user.getUsername());
        ct.andEqualTo("password", user.getPassword());

        List<User> users = mapper.selectByExample(example);

        if(users.size()>0){
            return users.get(0);
        }
        return null;
    }


    // 校验登录的时候用户名是否合法, 如果合法返回true 否则返回 false
    public boolean checkUserName(String username){
        UserMapper mapper = MyBatisUtils.getMapper(UserMapper.class);
        List<User> list = mapper.findUsersByUserName(username);
        MyBatisUtils.close();
         return list.size() != 0;
    }

}
